#include <arch/types.h>
#include <arch/Atomic.h>

void Atomic::add(dword *where, dword what) {
	asm volatile ("LOCK ADDL	%1, %0"
			:"=m" (*where)
			:"ir" (what), "m" (*where));
}

void Atomic::sub(dword *where, dword what) {
	asm volatile ("LOCK SUBL	%1, %0"
			:"=m" (*where)
			:"ir" (what), "m" (*where));
}

void Atomic::inc(dword *where) {
	asm volatile ("LOCK INCL	%0"
			:"=m" (*where)
			:"m" (*where));
}

void Atomic::dec(dword *where) {
	asm volatile ("LOCK DECL	%0"
			:"=m" (*where)
			:"m" (*where));
}

dword Atomic::TestAndSet(dword *where) {
	dword old;
	asm volatile ("LOCK BTS	$1, %1; SBBL	%0, %0"
			:"=r" (old)
			:"m" (*where)
			:"memory");
	return old;
}

